﻿local LibTweetCraft = LibStub:GetLibrary("LibTweetCraft-1.2")

local firstAddOnOffsetX = -25
local firstCategoryOffsetX = 30
local subsequentAddOnOffsetX = -30

local addOnOffsetY = -6
local firstCategoryOffsetY = -2
local subsequentCategoryOffsetY = 4

local checkButtonCount = 1

function CreateAutoTweetOptions(panel)
	-- Hide the current checkboxes, we're creating new noes
	HideChildren(panel)
	
	-- Get the addons
	local addOns = LibTweetCraft:GetAddOns()

	local lastCheckButton = nil

	-- Iterate through the addons
	for i = 1, #addOns, 1 do

		local addOn = addOns[i]
		local offsetX = 0
		
		-- If this is the first addon, we need to use firstAddOnOffsetX
		if i == 1 then
			offsetX = firstAddOnOffsetX
		-- Otherwise, if the previous addon had categories, we need to offset that
		elseif #(addOns[i - 1]:GetCategories()) > 0 then
			offsetX = subsequentAddOnOffsetX
		end
		
		-- Create CheckButton for addon (and store it as lastCheckButton for relativeTo)
		local addOnCheckButton = CreateAddOnCheckButton(panel, addOn, lastCheckButton or AutoTweetOptionsPanelSubText, offsetX, addOnOffsetY)
		
		lastCheckButton = addOnCheckButton
		
		-- Get the categories
		local categories = addOn:GetCategories()
		
		-- Iterate through the categories
		for j = 1, #categories, 1 do
		
			local category = categories[j]
			local offsetX, offsetY = 0, 0
			
			-- If this is the first category, we need to use firstCategoryOffsetX
			if j == 1 then
				offsetX = firstCategoryOffsetX
				offsetY = firstCategoryOffsetY				
			else
				offsetY = subsequentCategoryOffsetY
			end
			
			-- Create CheckButton for category (and store it as lastCheckButton for relativeTo)
			local categoryCheckButton = CreateCategoryCheckButton(panel, category, lastCheckButton, offsetX, offsetY)
			
			-- Set up dependency between check buttons
			BlizzardOptionsPanel_SetupDependentControl(addOnCheckButton, categoryCheckButton)
			
			lastCheckButton = categoryCheckButton
		end
	end
end

function HideChildren(panel)
	local children = { panel:GetChildren() };
	for _, child in ipairs(children) do
		child:Hide();
	end
end

function CreateAddOnCheckButton(panel, addOn, relativeTo, offsetX, offsetY)

	local addOnCheckButtonName = addOn.name .. "CheckButton" .. checkButtonCount
	checkButtonCount = checkButtonCount + 1
	
	if getglobal(addOnCheckButtonName) then
		return getglobal(addOnCheckButtonName)
	end
	
	-- Create CheckButton, if needed
	local addOnCheckButton = CreateFrame("Frame", addOnCheckButtonName, panel, "AutoTweetOptionsCheckButtonTemplate")
	
	-- Anchor the check button
	addOnCheckButton:SetPoint("TOPLEFT", relativeTo, "BOTTOMLEFT", offsetX, offsetY)
	
	-- Set the text and tooltip
	getglobal(addOnCheckButtonName .. "Text"):SetText(addOn.name)
	
	return addOnCheckButton
end

function CreateCategoryCheckButton(panel, category, relativeTo, offsetX, offsetY)

	local categoryCheckButtonName = category.name .. "CheckButton" .. checkButtonCount
	checkButtonCount = checkButtonCount + 1
	
	if getglobal(categoryCheckButtonName) then
		return getglobal(categoryCheckButtonName)
	end


	-- Create CheckButton
	local categoryCheckButton = CreateFrame("CheckButton", categoryCheckButtonName, panel, "AutoTweetOptionsSmallCheckButtonTemplate")
	
	-- Anchor the check button
	categoryCheckButton:SetPoint("TOPLEFT", relativeTo, "BOTTOMLEFT", offsetX, offsetY)
	
	-- Set the text and tooltip
	categoryCheckButton.text:SetText(category.displayName or category.name)
	categoryCheckButton.tooltipText = category.description or category.name
	
	-- Initialize the value
	categoryCheckButton:SetChecked(tweetCraftOptions.AddOns[category.addOn.name].Categories[category.name].Enabled)
	
	-- Hook the set function
	categoryCheckButton.setFunc = function(value)
		tweetCraftOptions.AddOns[category.addOn.name].Categories[category.name].Enabled = value == "1"
	end
	
	return categoryCheckButton
end
